<?php

namespace app\controller\Api\V1\Client;

use App\helper\Config;
use app\helper\GetProductInfo;
use app\model\ChannelCopyright;
use app\model\GameAuthorization;
use app\model\GameCopyrightInfo;
use app\model\Merchant;
use app\model\ToolServer;
use support\Request;
use support\Response;

class InfoController
{

    protected Config $configRe;
    protected GetProductInfo $getProductRe;


    public function __construct()
    {
        $this->configRe = Config::getInstance();
        $this->getProductRe = GetProductInfo::getInstance();
    }


    /**
     * 获取著作适龄信息
     *
     * @param Request $request
     * @return Response
     */
    public function GetInfo(Request $request): Response
    {

        // 获取游戏的著作版权信息
        $info = GameCopyrightInfo::query()->where('game', $request->input('game_id'))
            ->where('release_type', $request->input('release_type'))->first();
        if ($info) {
            $info = $info->toArray();

            return jsonRe([
                'ageAppropriateReminder' => $info['age_appropriate_reminder'],  // 16+适龄提示内容
                'copyrightIsShow' => (bool)$info['is_show_copyright'],  // 著作信息是否显示
                'copyrightOwner' => $info['owner_copyright'],  // 著作权人
                'copyrightNumber' => $info['copyright_number'],  // 著作权登记号
                'approvalNumber' => $info['approval_number'],  // 审批文号
                'publisher' => $info['publisher'],  // 出版单位
                'publicationNumber' => $info['publication_number'],  // 出版物号
                'ageAppropriateTip' => $info['age_appropriate_tip'],  // 适龄提示
                'otherTip' => $info['other_tip'],  // 其他提示
            ]);
        }


        // 没有就获取渠道的著作版权信息
        if ($request->input('release_type') === 1) {
            $gameInfo = GameAuthorization::query()->where('ulid', $request->input('game_id'))->first();

            if ($gameInfo) {
                $merchant = $gameInfo->merchant;
            }
        } else {
            if (!empty($request->input('mch_id'))) {
                $gameInfo = ToolServer::query()->where('ulid', $request->input('game_id'))->first();

                if ($gameInfo) {
                    $merchant = $gameInfo->organization;
                }
            } else {
                $merchant = null;
            }
        }

        if (!empty($merchant)) {
            $merchantInfo = Merchant::query()->where('ulid', $merchant)->first();

            if ($merchantInfo) {
                $info = ChannelCopyright::query()->where('channel', $merchantInfo->channel)->first();

                if ($info) {
                    $info = $info->toArray();

                    return jsonRe([
                        'ageAppropriateReminder' => $info['age_appropriate_reminder'],  // 16+适龄提示内容
                        'copyrightIsShow' => $info['copyright_is_show'],  // 著作信息是否显示
                        'copyrightOwner' => $info['copyright_owner'],  // 著作权人
                        'copyrightNumber' => $info['copyright_number'],  // 著作权登记号
                        'approvalNumber' => $info['approval_number'],  // 审批文号
                        'publisher' => $info['publisher'],  // 出版单位
                        'publicationNumber' => $info['publication_number'],  // 出版物号
                        'ageAppropriateTip' => $info['age_appropriate_tip'],  // 适龄提示
                        'otherTip' => $info['other_tip'],  // 其他提示
                    ]);
                }
            }
        }

        // 游戏和渠道都没有配置，获取官方通用的
        $copyrightIsShow = true;
        if ((int)$this->configRe->Get('game_copyright_is_show', 1)) {
            $hideGameList = json_decode($this->configRe->Get('game_copyright_hide_by_game'), true);

            if ($request->input('release_type')) {
                $gamePrefix = 'official_';
            } else {
                $gamePrefix = 'tool_';
            }

            $game = $gamePrefix . $request->input('game_id');

            if (in_array($game, $hideGameList ?? [])) {
                $copyrightIsShow = false;
            }
        } else {
            $copyrightIsShow = false;
        }

        return jsonRe([
            'ageAppropriateReminder' => $this->configRe->Get('game_age_appropriate_reminder', ''),  // 16+适龄提示内容
            'copyrightIsShow' => $copyrightIsShow,  // 著作信息是否显示
            'copyrightOwner' => $this->configRe->Get('game_copyright_owner', ''),  // 著作权人
            'copyrightNumber' => $this->configRe->Get('game_copyright_number', ''),  // 著作权登记号
            'approvalNumber' => $this->configRe->Get('game_approval_number', ''),  // 审批文号
            'publisher' => $this->configRe->Get('game_publisher', ''),  // 出版单位
            'publicationNumber' => $this->configRe->Get('game_publication_number', ''),  // 出版物号
            'ageAppropriateTip' => $this->configRe->Get('game_age_appropriate_tip', ''),  // 适龄提示
            'otherTip' => $this->configRe->Get('game_other_tip', ''),  // 其他提示
        ]);

    }

}